/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InputField.java
 *
 * Created on 16 Ιαν 2010, 10:59:08 πμ
 */
package forms;

import components.MyDraggable;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import receipts.Main;
import com.googlecode.svalidators.formcomponents.STextField;
import com.googlecode.svalidators.formcomponents.ValidationGroup;
import com.googlecode.svalidators.validators.SValidator;

/**
 *
 * @author lordovol
 */
public class InputField extends MyDraggable {

  private String title;
  private String[] fieldNames;
  public JTextField[] fields;
  public JLabel[] labels;
  private String value;
  public boolean cancel;
  private SValidator[] validators;
  private ValidationGroup group;

  /** Creates new form InputField */
  public InputField(String title, String[] fieldNames, SValidator[] validators) {
    this.title = title;
    this.fieldNames = fieldNames;
    this.validators = validators;
    fields = new JTextField[fieldNames.length];
    labels = new JLabel[fieldNames.length];
    initComponents();
    createFields();
    setLocationRelativeTo(null);
    setVisible(true);
    panel_main.validate();
    
  }

  private void createFields() {
    group = new ValidationGroup();
    for (int i = 0; i < fieldNames.length; i++) {
      final int vCounter = i;
      String labelName = fieldNames[i];
      JLabel l = new JLabel(labelName);
      l.setPreferredSize(new Dimension(100, 20));
      labels[i] = l;
      STextField j = new STextField(validators[i], validators[i].getValue());
      group.addComponent(j);
      j.setPreferredSize(new Dimension(200, 20));
      j.setName(labelName);
      fields[i] = j;
      JPanel p = new JPanel();
      p.setOpaque(false);
      p.add(labels[i]);
      p.add(fields[i]);
      panel_fields.add(p);

    }
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    panel_main = new javax.swing.JPanel();
    panel_fields = new javax.swing.JPanel();
    button_ok = new javax.swing.JButton();
    button_cancel = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

    jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    panel_main.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    panel_main.setPreferredSize(new java.awt.Dimension(389, 200));

    panel_fields.setLayout(new javax.swing.BoxLayout(panel_fields, javax.swing.BoxLayout.Y_AXIS));

    javax.swing.GroupLayout panel_mainLayout = new javax.swing.GroupLayout(panel_main);
    panel_main.setLayout(panel_mainLayout);
    panel_mainLayout.setHorizontalGroup(
      panel_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(panel_mainLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(panel_fields, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
        .addContainerGap())
    );
    panel_mainLayout.setVerticalGroup(
      panel_mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(panel_mainLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(panel_fields, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
        .addContainerGap())
    );

    button_ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
    button_ok.setToolTipText("OK");
    button_ok.setBorder(null);
    button_ok.setBorderPainted(false);
    button_ok.setContentAreaFilled(false);
    button_ok.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    button_ok.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        button_okActionPerformed(evt);
      }
    });

    button_cancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
    button_cancel.setToolTipText("Κλείσιμο");
    button_cancel.setBorder(null);
    button_cancel.setBorderPainted(false);
    button_cancel.setContentAreaFilled(false);
    button_cancel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    button_cancel.setFocusPainted(false);
    button_cancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        button_cancelActionPerformed(evt);
      }
    });

    jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+2));
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText(title);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(jPanel1Layout.createSequentialGroup()
            .addComponent(panel_main, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
            .addContainerGap())
          .addComponent(button_ok, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(button_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))))
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
          .addComponent(jLabel1)
          .addComponent(button_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(panel_main, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(button_ok, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void button_okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_okActionPerformed
      if(group.validate()){
      Main.glassPane.deactivate();
      dispose();
      } else {
        group.errorMessage(true);
      }
    }//GEN-LAST:event_button_okActionPerformed

    private void button_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_cancelActionPerformed
      Main.glassPane.deactivate();
      cancel = true;
      dispose();

    }//GEN-LAST:event_button_cancelActionPerformed
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton button_cancel;
  private javax.swing.JButton button_ok;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel panel_fields;
  private javax.swing.JPanel panel_main;
  // End of variables declaration//GEN-END:variables

  /**
   * @return the value
   */
  public String getValue() {
    return value;
  }
}
